<template>
  <div class="home">
    <!-- 导航 -->
    <div class="navBar">
      <navBar></navBar>
    </div>
    <!-- 主体 -->
    <div class="conten">
      <div class="adsForm">
        <h3 class="pageTitle">上传头像</h3>
        <!-- 链接广告 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="250px"
          class="demo-ruleForm"
          :v-show="tabIndex === 0"
        >
        <el-form-item label="在用头像" prop="name">
            <img src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" alt="" class="defaultImg">
          </el-form-item>
        
          <!-- 缩略图上传 -->
          <el-form-item label="缩略图上传" prop="name">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          
          <el-form-item class="btnOffset">
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确认修改</el-button
            >
            <el-button @click="resetForm('ruleForm')">返回首页</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 底部 -->
    <div class="footerBar">
      <footBar></footBar>
    </div>

    <!-- 右侧分享按钮 -->
  </div>
</template>


<script>
// @ is an alias to /sr
import navBar from "../../components/header";
import footBar from "../../components/footer";

export default {
  name: "imgUpload",
  components: {
    navBar,
    footBar,
  },
  data() {
    return {
      imageUrl: "",
    };
  },
  methods: {
    // 表单提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 缩略图上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped lang="less">
body,
htm {
  background-color: #999;
}

.footerBar {
  position: fixed;
  bottom: 0px;
  width: 100%;
}
.conten {
  padding-top: 60px;
   .pageTitle{
    font-size: 20px;
    color: #666;
    font-weight: bold;
    text-align: center;
    margin: 20px 0px 30px 0px;
  }
  .defaultImg{
    width: 100px;
    height: 100px;
    float: left;
    margin-left: 30px;
    border-radius: 6px;
  }
  > div {
    width: 600px;
    margin: 0 auto;
    background-color: #fff;
    padding: 20px 30px;
  }
  .unitStyle {
    color: cornflowerblue;
  }
  .tabBox {
    display: flex;
    justify-content: center;
    flex-wrap: nowrap;
    padding: 50px 0px 20px 0px;
    > div {
      font-size: 14px;
      font-weight: bold;
      color: #999;
      padding: 0 15px;
      height: 30px;
      line-height: 30px;
      cursor: pointer;
    }
    .active {
      background-color: #409eff;
      color: #fff;
    }
  }
  .backHome {
    font-size: 14px;
    color: #666;
    font-size: 14px;
    text-align: center;
    text-decoration: none;
  }
}

.longSelect {
  width: 100%;
}
.btnOffset{
 .el-form-item__content{
  margin-left: 50px;
 }
}
</style>

<style lang="less">
body,
htm {
  background-color: #f5f5f5;
}
// 缩略图上传
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border: 1px #bababa dashed;
  float: left;
  margin-left: 30px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.btnOffset{
 .el-form-item__content{
  margin-left: 50px!important;
 }
}
</style>